package com.lnsoft.cac.server.handle.webservice.MsgHandle;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HeartBeatXmlMsg {
    private Element rootNode;
    private Document doc;
    private Element cacNode;
    private Element devicesNode;

    public HeartBeatXmlMsg() {
        this.rootNode = new Element("request");
        this.doc = new Document(this.rootNode);
        this.cacNode = new Element("cac");
        this.rootNode.addContent(this.cacNode);
        this.devicesNode = new Element("devices");
        this.rootNode.addContent(this.devicesNode);
    }

    public void setCacNodeAttr(String strCACId, String strCACIp, String strCurtime, String fOperaTemperature) {
        this.cacNode.setAttribute("id",strCACId);
        this.cacNode.addContent(new Element("ip").setText(strCACIp));//添加子节点并赋值，添加到父节点
        this.cacNode.addContent(new Element("curtime").setText(strCurtime));
        this.cacNode.addContent(new Element("operationtemperature").setText(fOperaTemperature));
    }

    public void addDeviceNode(String deviceId,String status) {
        Element statusNode = new Element("status");
        statusNode.setText(status);
        Element deviceNode = new Element("device");
        deviceNode.setAttribute("id", deviceId);
        deviceNode.addContent(statusNode);
        this.devicesNode.addContent(deviceNode);
    }


    public String getMsg() {
        XMLOutputter XMLOut = new XMLOutputter(Format.getPrettyFormat());
        return XMLOut.outputString(this.doc);
    }
}
